package cc.comac.mom.util;

import java.sql.Connection;
import java.util.Properties;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import com.mysql.jdbc.Driver;

import javax.sql.DataSource;

@Component
public class BaseConnectionData {

	@Value("${spring.datasource.url}")
	private  String url;
	@Value("${spring.datasource.username}")
	private  String user;
	@Value("${spring.datasource.password}")
	private  String password;
	@Value("${spring.datasource.driverClassName}")
	private  String driver;

	@Autowired
	private DataSource dataSource;


	public  Connection getConnection() throws Exception {
		/*Driver forName = (Driver)Class.forName(driver).newInstance();
		Properties properties = new Properties();
		properties.put("user", user);
		properties.put("password", password);
		Connection connect = forName.connect(url, properties);*/
		Connection connect=dataSource.getConnection();
		return connect;
	}

	public  void closeConnection(Connection connect) throws Exception {
		try {
			if (connect != null) {
				connect.close();
			}
		}catch (Exception e){
			e.printStackTrace();
		}
	}
}
